#!/usr/bin/env python

import os,sys,shutil,commands,ConfigParser

def update():
    # Check svn
    svn = commands.getoutput("svn update")
    
    # If up to date tell the user
    if "At revision" in svn:
        print "already up to date"
        
    # If new version install it
    else:
        # Stop the daemon
        os.system("/etc/init.d/wattospm-daemon stop")
        
        # Kill the gui
        os.system("killall 'python /usr/bin/wattospm.py'")
        
        # Install the new version
        install("update")
        
        # Restart the daemon
        os.system("/etc/init.d/wattospm-daemon start")
        print "wattospm updated"
        
def uninstall():
    # Remove powermanagers group
    os.system("groupdel powermanagers")
    
    # Remove files
    if os.path.exists("/usr/bin/wattospm.py"):
        os.remove("/usr/bin/wattospm.py")        
    #for older version upgrade, remove in a couple versions 11/12/08
    if os.path.exists("/bin/wattospm.py"):
        os.remove("/bin/wattospm.py")
    if os.path.exists("/usr/bin/wattospm-daemon.py"):
        os.remove("/usr/bin/wattospm-daemon.py")
    if os.path.exists("/usr/bin/daemon.py"):
        os.remove("/usr/bin/daemon.py")
    if os.path.exists("/etc/init.d/wattospm-daemon"):
        os.remove("/etc/init.d/wattospm-daemon")
    if os.path.exists("/usr/share/wattospm"):
        shutil.rmtree("/usr/share/wattospm")
    if os.path.exists("/etc/wattospm"):
        shutil.rmtree("/etc/wattospm")
    if os.path.exists("/var/log/wattospm"):
        shutil.rmtree("/var/log/wattospm")
        
    # Remove daemon from startup
    os.system("update-rc.d wattospm-daemon remove")

def install(type):
    # Remove old settings
    uninstall()
    
    # Find username
    user = commands.getoutput("logname")
    
    # Set username in config file
    Config = ConfigParser.ConfigParser()
    configFile = open("config")
    Config.readfp(configFile)
    configFile.close()
    Config.set("config", "username", "%s" % (user,))
    Config.write(open("config","w"))
    
    # Create directories
    os.mkdir("/usr/share/wattospm")
    os.mkdir("/etc/wattospm")
    os.mkdir("/var/log/wattospm")
    
    # Create logfile
    os.system("touch /var/log/wattospm/log")
    
    # Copy files
    shutil.copy("wattospm.py","/usr/bin/wattospm.py")
    shutil.copy("wattospm-daemon.py", "/usr/bin/wattospm-daemon.py")
    shutil.copy("daemon.py","/usr/bin/daemon.py")
    shutil.copy("wattospm-daemon", "/etc/init.d/wattospm-daemon")
    shutil.copy("wattospm.glade","/usr/share/wattospm/wattospm.glade")
    shutil.copy("stock.png","/usr/share/wattospm/stock.png")
    shutil.copy("performance.png","/usr/share/wattospm/performance.png")
    shutil.copy("power_saving.png","/usr/share/wattospm/power_saving.png")
    shutil.copy("config","/etc/wattospm/config")
    shutil.copy("config.default","/etc/wattospm/config.default")
    
    # Make files executable
    os.system("chmod +x /usr/bin/wattospm.py")
    os.system("chmod +x /usr/bin/wattospm-daemon.py")
    os.system("chmod +x /etc/init.d/wattospm-daemon")
    
    # Set daemon to run at start
    os.system("update-rc.d wattospm-daemon defaults")
    
    # Create powermanagers group and add user
    os.system("groupadd powermanagers")
    os.system("usermod -a -G powermanagers %s" % (user,))
    
    # Set permissions on config file
    os.system("chown root:powermanagers /etc/wattospm/config")
    os.system("chmod 770 /etc/wattospm/config")
    if type == "new":
        print "please restart before running wattospm"
   
for arg in sys.argv:
    if arg == "update":
        update()
    if arg == "install":
        install("new")
    if arg == "uninstall":
        uninstall()